<template>
  <el-dialog :visible.sync="visible" title="投标单位注册" :close-on-click-modal="false" :close-on-press-escape="false" width='35%'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="140px">
        <input type="password" class="hide" id="loginPassword"/>
        <input type="text" class="hide" id="loginUserName"/>
        <el-form-item label="用户名" prop="username">
            <el-input v-model="dataForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="dataForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="comfirmPassword">
            <el-input type="password" v-model="dataForm.comfirmPassword"></el-input>
        </el-form-item>
        <el-form-item label="单位名称" prop="unitName">
            <el-input v-model="dataForm.unitName"></el-input>
        </el-form-item>
         <el-form-item label="社会统一信用代码" prop="unitCode">
            <el-input v-model="dataForm.unitCode"></el-input>
        </el-form-item>
         <el-form-item label="联系人" prop="unitContacts">
            <el-input v-model="dataForm.unitContacts"></el-input>
        </el-form-item>
          <el-form-item label="联系电话" prop="unitPhone">
            <el-input v-model="dataForm.unitPhone"></el-input>
        </el-form-item>
          <el-form-item label="联系邮箱" prop="email">
            <el-input v-model="dataForm.email"></el-input>
        </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">注册</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { isEmail, isMobile } from '@/utils/validate'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        username: '',
        password: '',
        comfirmPassword: '',
        unitName: '',
        unitCode: '',
        unitContacts: '',
        unitPhone: '',
        email: ''
      }
    }
  },
  computed: {
    dataRule () {
      var validateComfirmPassword = (rule, value, callback) => {
        if (this.dataForm.password !== value) {
          return callback(new Error('两次密码输入不一致'))
        }
        callback()
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          return callback(new Error(this.$t('validate.format', { 'attr': this.$t('user.mobile') })))
        }
        callback()
      }
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          return callback(new Error(this.$t('validate.format', { 'attr': this.$t('user.email') })))
        }
        callback()
      }
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unitName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unitContacts: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unitPhone: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        unitCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        email: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateEmail, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 16, message: '密码长度在 5 到 16 个字符', trigger: 'blur' }
        ],

        comfirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { min: 5, max: 16, message: '密码长度在 5 到 16 个字符', trigger: 'blur' },
          { validator: validateComfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
    },

    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/user/register', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: '注册成功',
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss" scoped>
        .hide{
            width: 0;
            position: absolute;
            border:none;
        }
</style>
